/*
 * 本类作用：数据操作,测试类
 * 
 * 作者：袁小杰
 * 博客：http://blog.csdn.net/undoner
 * GIT：https://git.oschina.net/undoner
 * QQ：15137281
 * 
 */
package org.mina.business.db;

import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mina.business.dao.GpsCarInfoDAO;
import org.mina.business.dao.TestGpsDataDAO;
import org.mina.business.model.Gps_carinfo;
import org.mina.business.model.TestGpsData;

public class TestMybatis {
	private static SqlSessionFactory sqlSessionFactory;
	private static Reader reader;

	static {
		try {
			String filePath= System.getProperty("user.dir")+ File.separator + "config"
					+ File.separator +"\\MybatisConfig.xml";
			//绝对路径获取文件：
			reader = new FileReader(new File(filePath));
			//如果是src目录下，可使用以下路径：
			//reader = Resources.getResourceAsReader(filePath);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static SqlSessionFactory getSession() {
		return sqlSessionFactory;
	}
	
	public static List<Gps_carinfo>  getGps_carinfo() {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			GpsCarInfoDAO Gpsinfo = session.getMapper(GpsCarInfoDAO.class);
			List<Gps_carinfo> list = Gpsinfo.getGpsCarInfo();
			if (list != null) {
				for (Gps_carinfo gpscarinfo : list) {
					System.out.println(gpscarinfo.getCarinfo()+""+gpscarinfo.getGpsname());
				}
			}
		} finally {
			session.close();
		}
		return null;
	}

	
	public static List<TestGpsData>  getGpsData() {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			TestGpsDataDAO gpsDataCeshiMapper = session.getMapper(TestGpsDataDAO.class);
			List<TestGpsData> list = gpsDataCeshiMapper.getGpsData();
			if (list != null) {
				for (TestGpsData gpsData : list) {
					System.out.println(gpsData.getTime());
				}
			}
		} finally {
			session.close();
		}
		return null;
	}

	
	public static void insertGpsDataCeshi() {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			TestGpsDataDAO gpsDataCeshiMapper = session.getMapper(TestGpsDataDAO.class);
			TestGpsData gpsDataCeshi = new TestGpsData("2014-04-11 10:19:20.522","13862082464","116.98594","033.60277","118.4631");
			gpsDataCeshiMapper.insertGpsData(gpsDataCeshi);
			session.commit();// 这里一定要提交，不然数据进不去数据库中
			System.out.println("getTime: " + gpsDataCeshi.getTime());
		} finally {
			session.close();
		}
	}

	public static void main(String[] args) {
		//getGps_fence();
		getGps_carinfo();
	}

}
